#include "hbsender.h"
#include "log.h"

#include <QUdpSocket>
#include <QTimer>
#include <QByteArray>
#include <QDataStream>
#include <QDateTime>
#include <QtTest/QtTest>

HBSender :: HBSender(){
    criarSocket();
    conectarSocket();
}

QBool HBSender :: criarSocket(){
    socket = new QUdpSocket( this );
    qDebug("hbsend will connect ...");
    return QBool(true);
}

QBool HBSender::conectarSocket()
{
  socket->connectToHost( QHostAddress::Broadcast, 9988 );
  qDebug("hbsend connected!");
  QTimer *timer = new QTimer( this );
  timer->setInterval( 1000 );
  timer->start();
  connect( timer, SIGNAL(timeout()), this, SLOT(broadcastLine()) );
  return QBool(true);
}

QString HBSender::broadcastLine()
{

  const char *msg = "";
  QDateTime dt = QDateTime::currentDateTime();
  qDebug("hbsend  sending hearbeat" + dt.toString().toLatin1());
  socket->write( msg, 0 );
  return "enviado";
  //socket->disconnectFromHost();
  return "fim";
}
void HBSender :: criarSocketTest(){
    QBool criaTest = criarSocket();
    QCOMPARE(criaTest, QBool(true));
}

void HBSender :: conectarSocketTest(){
    QBool conectaTesta = conectarSocket();
    QCOMPARE(conectaTesta, QBool(true));
}

void HBSender::broadcastLineTest()
{
    QString str = broadcastLine();
    QCOMPARE(str.toUpper(), QString("ENVIADO"));
}

